package com.ybox.crawler.service;

import com.ybox.crawler.domain.Article;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

@Component
public class DataPipeline implements Pipeline {

    private static String path = "D:/java/test.html";

    @Override
    public void process(ResultItems resultItems, Task task) {
        // 获取需要保存到MySQL的数据
        Article article = resultItems.get("article");

        // 判断获取到的数据不为空
        if(article!=null) {
            try {
                // 如果有值则进行保存
                File txt=new File(path);
                if(!txt.exists()){
                    txt.createNewFile();
                }
                FileOutputStream fileOutputStream = new FileOutputStream(txt);
                byte bytes[] = new byte[512];
                bytes = article.getResultHtml().getBytes();
                fileOutputStream.write(bytes);
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }
}
